<?php
define('FPDF_FONTPATH', 'font/');
require('./font/fpdf.php');
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
  $pdf=new FPDF('P','mm','A4');   //Create new pdf file
  $pdf->Open();     //Open file
  $pdf->SetAutoPageBreak(false);  //Disable automatic page break
  
  $pdf->AddPage();  //Add first page
  
  $pdf->SetFillColor(232, 232, 232);
	$pdf->SetFont('Arial', '', 10);
  $pdf->SetXY(5,6);
  $pdf->Cell(85,6,"Party Name", 1,0,'C',1);
  $pdf->Cell(25,6,"Mobile No", 1,0,'C',1);
  $pdf->Cell(30,6,"City", 1,0,'C',1);
  $pdf->Cell(30,6,"Jama Rupiya", 1,0,'C',1);
  $pdf->Cell(30,6,"Udhar Rupiya", 1,0,'C',1);
  
  $yAxis = 12;
  $rowHeight = 6;
  $usedPageSize = 0;
  
  for($i=0; $i < count($_POST['checkedParty']); $i++)
  {
 		if($usedPageSize == 270)
 		{
      $yAxis        = 12;
      $usedPageSize = 0;   
      $pdf->AddPage();  //Add first page  			
   	  printData($_POST['checkedParty'][$i],$yAxis,$rowHeight);
   	  $yAxis += $rowHeight;
 		}
 		else
 		{
   	  printData($_POST['checkedParty'][$i],$yAxis,$rowHeight);
   	  $yAxis += $rowHeight;
   	  $usedPageSize += 6;
 		}
  }
  $pdf->Output();
}

function printData($checkedParty,$yAxis,$rowHeight)
{
	
	global $pdf;
	$jamaRupiyaTotal  = 0;
	$udharRupiyaTotal = 0;
	$total            = 0;
	
  $selectParty = "SELECT partyId,partyName,mobile1,city
                    FROM party
               LEFT JOIN city ON city.cityId = party.cityId
                   WHERE partyId = ".$checkedParty;
  $selectPartyRes = mysql_query($selectParty);
  while($partyRow = mysql_fetch_array($selectPartyRes))
  {
  	$i=0;
    $select="SELECT transaction.partyId,jamaRupiya,udhaarRupiya
               FROM transaction
              WHERE partyId=".$partyRow['partyId'];
    $res = mysql_query($select); 
    while($row = mysql_fetch_array($res)) 	
    {           
    	$jamaRupiyaTotal  += $row['jamaRupiya'];
    	$udharRupiyaTotal += $row['udhaarRupiya'];
    	$total             = $jamaRupiyaTotal - $udharRupiyaTotal;
      $i++;
    }
  	$pdf->SetFillColor(232, 232, 232);
    $pdf->SetFont('Arial', '', 8.8);
    $pdf->SetXY(5,$yAxis);
    $pdf->Cell(85,6,$partyRow['partyName'], 1,0,'L');
    $pdf->Cell(25,6,$partyRow['mobile1'],1,0,'L');
    $pdf->Cell(30,6,$partyRow['city'],1,0,'L');
    if($total > 0)
    {
      $pdf->Cell(30,6,$total,1,0,'R');
    }
    else
    {
    	$pdf->Cell(30,6,0,1,0,'R');
    }
    if($total < 0)
    {
      $pdf->Cell(30,6,$total,1,0,'R');
    }
    else
    {
    	$pdf->Cell(30,6,0,1,0,'R');
    }
  }
}
?>